import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { CreateGanttWizardWindow } from "../customWindows/createGanttWizard";
import { ganttConfig } from "@/mdesign-lib/mdesign-app.umd.js";
import { archService } from "@/mdesign-lib/mdesign-app.umd.js";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";

export class CreateGanttWizardAction extends Action {
  keyName = "createGanttWizard" as ActionKey;
  desc = "createGanttWizard";

  run(projectId: string, option: any) {
    const defaultOwnerId = option.ownerId;
    const stereotype = option.appliedStereotypes[0];
    const createGanttParams = ganttConfig[stereotype].createGanttParams;
    CreateGanttWizardWindow.popup(
      {
        projectId,
        stereotype,
        defaultTableName: option.defaultTableName,
        defaultOwnerId,
        stepKeys: createGanttParams?.stepKeys || [],
        moreBtnCondition: createGanttParams?.moreBtnCondition,
        moreBtnConditions: createGanttParams?.moreBtnConditions
      },
      async ({ success, result }) => {
        if (!success) return;
        console.log("result", result);
        const { name, ownerId, existModelIds, style } = result;
        await archService
          .createGantt({
            projectId,
            defaultName: name,
            parentId: ownerId || option.ownerId,
            applyStereotypes: option.appliedStereotypes,
            metaclass: option.metaclass,
            existModelIds,
            style
          })
          .finally(() => {
            CreateGanttWizardWindow.done();
          });
      }
    );
  }
}
